<?php

namespace app\api\model;
use app\common\model\ProjectTasks as ProjectTasksModel;
class ProjectTasks extends ProjectTasksModel
{
    /**
     * 查询列表
     * @param $type
     * @param $user_id
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function list($type,$user_id){
        $where = [];
        switch ($type){
            case 'finish':
                $where['entry_status']=self::REVIEWED;
                break;
            case 'not_finish':
                $where['entry_status'] = ['<>',3];
                break;
        }
        $where['user_id']=$user_id;
        return $this->where($where)->order('id desc')
        ->paginate(15, false, [
            'query' => \request()->request()
        ]);
    }
    /**
     * 返回想起信息
     */
    public function detail($id){
        $data = $this->where(['id'=>$id])->with(['material'])->find();
        if(!empty($data['construction_front'])){
            $data->construction_front = json_decode($data->construction_front,true);
        }
        if(!empty($data['construction_in'])){
            $data->construction_in = json_decode($data->construction_in,true);
        }
        if(!empty($data['construction_finish'])){
            $data->construction_finish = json_decode($data->construction_finish,true);
        }
        return $data;

    }

    /**
     * @param $data
     * @return false|int
     */
    public function editInfo($data){
        if(!empty($data['construction_front']) && !empty(htmlspecialchars_decode($data['construction_front']))){
            $data['constate']=1;
            $data['construction_front'] = htmlspecialchars_decode($data['construction_front']);
        }
        if(!empty($data['construction_in']) && !empty(htmlspecialchars_decode($data['construction_in']))){
            $data['constate']=2;
            $data['construction_in'] = htmlspecialchars_decode($data['construction_in']);

        }
        if(!empty($data['construction_finish']) && !empty(htmlspecialchars_decode($data['construction_finish']))){
            $data['entry_status'] = self::WANCHENG;
            $data['constate']=3;
            $data['construction_finish'] = htmlspecialchars_decode($data['construction_finish']);
        }
        return $this->save($data);
    }

    /**
     * 查询已完成订单
     * @param $searchtype
     * @param $user_id
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function search($searchtype,$user_id){
        $starttime=$endtime='';
        switch ($searchtype){
            case 'day':
                $starttime = strtotime(date('Y-m-d 00:00:00',time()));
                $endtime = strtotime(date('Y-m-d 23:59:59',time()));
                break;
            case 'month':
                $firstday = date('Y-m-01', time());
                $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                $starttime = strtotime($firstday);
                $starttime = strtotime($lastday);
        }
        $where['user_id']=$user_id;
        $where['create_time'] = ['between time', [$starttime, $endtime]];
        $where['entry_status'] = self::REVIEWED;
        return $this->where($where)->order('id desc')
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }
    //月订单、日订单
    public function orderTotal($user_id){
        $starttimed = strtotime(date('Y-m-d 00:00:00',time()));
        $endtimed = strtotime(date('Y-m-d 23:59:59',time()));
        $firstday = date('Y-m-01', time());
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        $starttimem = strtotime($firstday);
        $starttimem = strtotime($lastday);
        $wherey['entry_status']=$whered['entry_status'] = self::REVIEWED;
        $whered['create_time'] = ['between time', [$starttimed, $endtimed]];
        $day = $this->where($whered)->count('id');
        $wherey['create_time'] = ['between time', [$starttimem, $starttimem]];
        $mounth =  $this->where($wherey)->count('id');
        return ['day_total'=>$day,'mounth_total'=>$mounth];
    }

}